import { reactive, toRef } from 'vue';
import { V_USER } from '@/api/url/erp/baseMessage';
import { getjhd } from '@/api/url/erp/logisticDocuments';
import { sliceTime } from '@/utils/time';
export default function importConfig() {
  const base = reactive<any>({
    formData: {
      MF001: '', // 用户编号
      MF002: '', // 用户名称
      MF007: '', // 部门编号
      MODI_DATE: '', // 最后修改时间
    },
    columns: [
      {
        title: '入库单编号',
        dataIndex: 'purID',
        checked: true,
      },
      {
        title: '供应商名称',
        dataIndex: 'tg021',
        checked: true,
      },
      {
        title: '入库时间',
        dataIndex: 'tg003',
        checked: true,
      },
      {
        title: '采购订单号',
        dataIndex: 'cgId',
        checked: true,
      },
      {
        title: '单据状态',
        dataIndex: 'tg013',
        checked: true,
      },
      {
        title: '操作',
        slotName: 'optional',
        fixed: 'right',
        width: 290,
        checked: true,
      },
    ],
    data: [],
    purtgData: [],
    rules: {
      MF001: [
        {
          required: true,
          message: '请输入用户编号',
        },
      ],
      MF002: [
        {
          required: true,
          message: '请输入用户名称',
        },
      ],
      MF007: [
        {
          required: true,
          message: '请输入部门编号',
        },
      ],
    },
    config: ['add'],
    searchBar: '隐藏搜索',
    
    ModelTitle: '新增用户',
    visible: false, //遮罩层显示
    delVisible: false, //删除用户遮罩层
    delUserMsg: {},
    handleSubmit: null,
    upMsgIndex: 0, // 修改的下标
    selectFormData: {
      MF001: '', // 用户编号
      MF002: '', // 用户名称
      MF007: '', // 部门编号
      MF025: 'P'
    }, // 搜索表单
    param: {
      page: 1,
      pageSize: 20,
      total: 0,
    }
  });

  // 选择时间
  const onOk = (dateString, date) => {
    base.userFormData.MODI_DATE = dateString;
  };

  // 初始化userFormData
  const initUserFormData = {
    MF001: '',
    MF002: '',
    MF007: '',
    MODI_DATE: '',
  };
  const handleClick = () => {
    base.visible = true;
    base.userFormData = initUserFormData;
    base.ModelTitle = '新增用户';
  };

  // commit
  const handleBeforeOk = (done) => {
    base.handleSubmit.validate().then((res) => {
      if (!res) {
        console.log(base.upMsgIndex);
        if (base.upMsgIndex != null) {
          // 修改用户
          base.upMsgIndex = null;
        } else {
          // 添加用户
          // base.data.push(base.userFormData);
        }
        done();
      } else {
        done(false);
      }
    });
  };

  // 审核码
  const auditCode = {
    Y: '已审核', 
    N: '未审核',
    V: '作废'
  }

  // 读取数据
  getjhd(base.param).then((res) => {
    
    if(res.code == '200'){
      base.data = res.data.list.map((item: any) => {
        base.purtgData.push(item.purtg)
        
        return item;
      });
      base.purtgData.map((item: any) => {
        item.purID = item.tg001 + "-" + item.tg002;
        item.cgId = item.tg047 + "-" + item.tg048;
        item.tg003 = sliceTime(item.tg003).replaceAll(":","");
        item.tg013 = auditCode[item.tg013];
        return item;
      })
    }
    
  });

  // V_USER().then((res) => {
  //   console.log(res);
  //   if (res.code == 200 && res.data) {
  //     base.data = res.data.map((item) => {
  //       item.MODI_DATE = sliceTime(item.MODI_DATE);
  //       return item;
  //     });
  //   }
  // });

  const handleCancel = () => {
    base.visible = false;
  };

  const upUser = (record, rowIndex) => {
    console.log(record);
    base.visible = true;
    base.ModelTitle = '修改用户信息';
    base.userFormData = record;
    base.upMsgIndex = rowIndex;
  };

  const delUser = (record) => {
    console.log(record);
    base.delVisible = true;
    base.delUserMsg = record;
  };

  // 确实删除页面删除
  const delHandleOk = () => {
    console.log('删除');
    console.log(base.delUserMsg);
    base.delVisible = false;
  };

  // 确实删除页面取消
  const delHandleCancel = () => {
    console.log('取消');
    base.delUserMsg = {};
    base.delVisible = false;
  };

  // 查询
  const selectDataMsg = () => {
    console.log(base.selectFormData);
  };

  // 重置SelectFormData
  const resetSelectFormData = () => {
    base.selectFormData.MF001 = '';
    base.selectFormData.MF002 = '';
    base.selectFormData.MF007 = '';
  };
  return {
    base,
    onOk,
    handleClick,
    handleBeforeOk,
    handleCancel,
    upUser,
    delUser,
    delHandleOk,
    delHandleCancel,
    selectDataMsg,
    resetSelectFormData,
  };
}
